package com.rainmonth.nicehair.ui.fragment;

import java.util.LinkedList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Bind;

import com.rainmonth.nicehair.R;
import com.rainmonth.nicehair.ui.activity.HairCommentActivity;
import com.rainmonth.nicehair.adapter.FindHairAdapter;
import com.rainmonth.nicehair.common.json.FindHairParser.HairResult;
import com.rainmonth.nicehair.common.model.FindHairItem;
import com.rainmonth.nicehair.common.net.ActionOfUrl.JsonAction;
import com.rainmonth.nicehair.common.net.NetAsyncTask;
import com.rainmonth.nicehair.utils.AppToast;
import com.rainmonth.nicehair.utils.HProgress;
import com.rainmonth.nicehair.utils.ImageFetcher;
import com.rainmonth.nicehair.waterfall.base.PLA_AdapterView;
import com.rainmonth.nicehair.waterfall.base.PLA_AdapterView.OnItemClickListener;
import com.rainmonth.nicehair.waterfall.widget.XListView;
import com.rainmonth.nicehair.waterfall.widget.XListView.IXListViewListener;

public class FindHairFragment extends Fragment implements IXListViewListener {

    @Bind(R.id.water_list)
    XListView mWaterList;
    private LinkedList<FindHairItem> mListInfos = new LinkedList<FindHairItem>();
    private FindHairAdapter mAdapter;
    private ImageFetcher mImageFetcher;
    private int currentPage = 1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case R.id.ui_show_dialog:
                    //dialog.show();
                    HProgress.show(getActivity(), null);
                    break;
                case R.id.ui_dismiss_dialog:
                    //dialog.dismiss();
                    HProgress.dismiss();
                    break;
                case R.id.ui_show_text://网络超时和数据解析异常
                    if (getActivity() != null)
                        AppToast.showShortText(getActivity(), msg.arg1);
                    break;
                case R.id.ui_update_ui:
                    break;
            }
        }
    };

    public static FindHairFragment newInstance(int position) {
        FindHairFragment fragment = new FindHairFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.view_hair_recomm, container, false);
        ButterKnife.bind(this, contentView);
        mWaterList.setPullLoadEnable(true);
        mWaterList.setXListViewListener(this);
        mImageFetcher = new ImageFetcher(getActivity(), 300);
        mImageFetcher.setLoadingImage(R.drawable.hairscan_loading);
        mAdapter = new FindHairAdapter(getActivity(), mImageFetcher);
        mWaterList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(PLA_AdapterView<?> parent, View view,
                                    int position, long id) {
                if (position < 1) return;
                FindHairItem info = mAdapter.mListInfos.get(position - 1);
                Intent intent = new Intent(getActivity(), HairCommentActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("hairInfo", info);
                intent.putExtras(mBundle);
                startActivity(intent);
            }
        });
        parseArgument();
        return contentView;
    }

    private void parseArgument() {
        mWaterList.setAdapter(mAdapter);
        Bundle bundle = getArguments();
        int position = bundle.getInt("position");
        switch (position) {
            case 0:
            case 1:
                AddItemToContainer(++currentPage, 1);
                break;
        }
    }

    public class NetContent extends NetAsyncTask {

        private HairResult result;
        private int mType = 1;

        public NetContent(Handler uiHandler, int type) {
            super(uiHandler);
            this.mType = type;
            if (mListInfos.size() == 0)
                setDialogId(1);
        }

        @Override
        protected void handlePreExecute() {

        }

        @Override
        protected String handleNetworkProcess(String... arg0) throws Exception {
            result = (HairResult) httptask.getRequestbyPOST(JsonAction.FINDHAIR, arg0[0], null);
            return result != null ? HANDLE_SUCCESS : HANDLE_FAILED;
        }

        @Override
        protected void handleResult() {
            if (result != null && result.isSuccess()) {
                mListInfos = result.getmListInfo();
                if (mType == 1) {
                    mAdapter.mListInfos = mListInfos;
                    mAdapter.notifyDataSetChanged();
                    mWaterList.stopRefresh();
                } else if (mType == 2) {
                    mWaterList.stopLoadMore();
                    mAdapter.addItemLast(mListInfos);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }

    }

    private void AddItemToContainer(int pageindex, int type) {
        String url = "album/1733789/masn/p/" + pageindex + "/24/";
        new NetContent(mHandler, type).execute(url);
    }

    @Override
    public void onResume() {
        super.onResume();
        mImageFetcher.setExitTasksEarly(false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        AddItemToContainer(currentPage, 1);

    }

    @Override
    public void onLoadMore() {
        AddItemToContainer(++currentPage, 2);

    }
}
